<template>
  <div>
    <!-- <el-row style="margin: 20px 0">
        <el-col :span="7">
          <el-input v-model="key" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="8" style="margin: 0 10px">
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator=","
            start-placeholder="Start date"
            end-placeholder="End date"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" @click="search">搜索</el-button>
        </el-col>
      </el-row> -->
    <el-button type="primary" @click="openModal">添加</el-button>
    <el-dialog
      :title="tag === 0 ? '添加' :'修改'"
      :visible.sync="dialogFormVisible"
      @close="close"
    >
      <el-form :model="myNoticeTableData">
        <el-form-item label="公告标题">
          <el-input v-model="myNotice.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面图">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/api/my/notify/fileup"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="公告内容">
          <el-input v-model="myNotice.content" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="myNoticeTableData" style="width: 100%">
      <el-table-column
        prop="title"
        label="公告标题"
        width="180"
      ></el-table-column>
      <el-table-column prop="content" label="公告内容"></el-table-column>
      <el-table-column prop="picture" label="公告封面">
        <template slot-scope="scope">
          <div class="img-container">
            <img
              :src="`http://localhost:8080/publicWelfare/down?fileName=${scope.row.picture}`"
              alt=""
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="上传时间"> </el-table-column>
      <!-- <el-table-column prop="lastUpdateTime" label="最后修改时间">
        </el-table-column> -->
      <el-table-column width="180" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除此条记录" @confirm="handleDelete(scope.row)">
            <el-button
              size="mini"
              type="danger"
              slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      sytl
      layout="prev, pager, next"
      :total="totalCount"
      :current-page="currentPage"
      :on-success="handleAvatarSuccess"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>
<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      key: "",
      date: [],
      tag: 0,
      typeOptions: [],
      typeValue: "",
      myNoticeTableData: [],
      dialogFormVisible: false,
      myNotice: {
        title: "",
        content: "",
        picture: "",
        time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
        lastUpdateTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
      },
      imageUrl: "",
      totalCount: 0, // 总记录数
      pageSize: 10, // 每页显示的记录数
      currentPage: 1, // 当前页码
    };
  },
  methods: {
    /**
     * 打开模态框
     */
    openModal() {
      this.tag = 0;
      this.dialogFormVisible = !this.dialogFormVisible;
    },

    /**
     * 搜索
     */
    search() {
      var startTime = "";
      var endTime = "";
      console.log(this.date);

      if (this.date.length != 0) {
        let [start, end] = this.date;
        if (start) {
          startTime = dayjs(start).format("YYYY-MM-DD HH:mm:ss");
        }
        if (end) {
          endTime = dayjs(end).format("YYYY-MM-DD HH:mm:ss");
        }
      }

      this.$http
        .get(
          `http://localhost:8080/publicWelfare/searchpublicWelfare?q=${this.key}&starTime=${startTime}&endTime=${endTime}`
        )
        .then((res) => {
          console.log(res);
          this.publicWelTableData = res.data.data.list;
          this.totalCount = res.data.data.total;
        });
    },
    /**
     * 关闭模态框
     */
    close() {
      this.imageUrl = "";
    },
    /**
     * 添加文章
     */
    handleAdd() {
      this.dialogFormVisible = false;
      if (this.tag === 1) {
        this.$http
          .put("http://localhost:8080/api/my/notify/updateNotify", {
            ...this.myNotice,
          })
          .then((res) => {
            this.$http
              .get(
                `http://localhost:8080/api/my/notify/getNotifys?pageNo=${this.currentPage}&pageSize=${this.pageSize}`
              )
              .then((res) => {
                console.log(res);
                this.myNoticeTableData = res.data.data.list;
                this.totalCount = res.data.data.total;
              })
              .catch((e) => console.log(e));
          });
      } else {
        this.$http
          .post("http://localhost:8080/api/my/notify/addNotify", {
            ...this.myNotice,
          })
          .then((res) => {
            this.$http
              .get(
                `http://localhost:8080/api/my/notify/getNotifys?pageNo=${this.currentPage}&pageSize=${this.pageSize}`
              )
              .then((res) => {
                console.log(res);
                this.myNoticeTableData = res.data.data.list;
                this.totalCount = res.data.data.total;
              })
              .catch((e) => console.log(e));
          });
      }
    },

    handleEdit(e) {
      this.tag = 1;
      this.dialogFormVisible = true;
      this.myNotice = e;
      this.imageUrl = `http://localhost:8080/publicWelfare/down?fileName=${e.picture}`;
    },
    /**
     * 分页跳转
     * @param newPage
     */
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.$http
        .get(
          `http://localhost:8080/article/getAllArticles?pageNum=${this.currentPage}&pageSize=${this.pageSize}`
        )
        .then((res) => {
          this.publicWelTableData = res.data.data.list;
          this.totalCount = res.data.data.total;
        });
    },
    /**
     * 回传图片链接
     * @param id
     */
    handleAvatarSuccess(res, file) {
      this.myNotice.picture = res.data;
      console.log(this.articleFormData);
    },
    /**
     *
     * @param file 回显图片
     */
    beforeAvatarUpload(file) {
      var objectURL = URL.createObjectURL(file);
      this.imageUrl = objectURL;
    },

    handleDelete(e) {
      this.$http
        .delete(`http://localhost:8080/api/my/notify/deleteNotify/${e.id}`)
        .then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.$http
            .get(
              `http://localhost:8080/api/my/notify/getNotifys?pageNo=${this.currentPage}&pageSize=${this.pageSize}`
            )
            .then((res) => {
              console.log(res);
              this.myNoticeTableData = res.data.data.list;
              this.totalCount = res.data.data.total;
            })
            .catch((e) => console.log(e));
        });
    },
  },

  computed: {
    displayRow() {
      // console.log(this.fmTableData.filter(item => item.fmStatus === 1));
      return this.articleTableData.filter((item) => item.status === 1);
    },
  },

  created() {
    this.$http
      .get(
        `http://localhost:8080/api/my/notify/getNotifys?pageNo=${this.currentPage}&pageSize=${this.pageSize}`
      )
      .then((res) => {
        console.log(res);
        this.myNoticeTableData = res.data.data.list;
        this.totalCount = res.data.data.total;
      })
      .catch((e) => console.log(e));
  },
};
</script>

<style>
.img-container {
  width: 70px;
  height: 70px;
  position: relative;
  border-radius: 20%;
}

.img-container > img {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  object-fit: cover;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 20%;
}

.play-icon {
  font-size: 30px;
  line-height: 70px;
  margin: 0 auto;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
